﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teacker_kit_Blank_App.DataModel;

namespace Teacker_kit_Blank_App.Engine.Database
{
   public class DatabaseManager
    {
      
       private DatabaseManager()
       {
       }

       private static DatabaseManager instance ;
       public static DatabaseManager GetInstance()
       {
           if (instance == null)
           {
               instance = new DatabaseManager();
           }
           return instance;
       }


       public string DBPath { get; set; }

       //this will create student , class sample database ,later we will need to make it generic or load the database strucure
       public void CreateDatabase()
       {
           //check dbpath is set here
           using (var db = new SQLite.SQLiteConnection(this.DBPath))
           {
               // Create the tables if they don't exist
               db.CreateTable<Class>();
               db.CreateTable<Student>();
           }
       }

       public void InsertStudent(Student st)
       {
           //check dbpath is set here
           using (var db = new SQLite.SQLiteConnection(this.DBPath))
           {
               db.Insert(st);
           }
       }

       public void UpdateStudent(Student st)
       {
           //check dbpath is set here
           using (var db = new SQLite.SQLiteConnection(this.DBPath))
           {
               db.Update(st);
           }
       }

       public void DeleteStudent(Student st)
       {
           //check dbpath is set here
           using (var db = new SQLite.SQLiteConnection(this.DBPath))
           {
               db.Delete(st);

           }
       }

       public List<Student> ReadStudents()
       {
           //check dbpath is set here
           using (var db = new SQLite.SQLiteConnection(this.DBPath))
           {
               var query = db.Table<Student>().OrderBy(s => s.Name);
               List<Student> studentList = new List<Student>(  query.AsEnumerable<Student>());
               return studentList;
              /* foreach (var _student in query)
               {
                   var customer = new CustomerViewModel()
                   {
                       Id = _customer.Id,
                       Name = _customer.Name,
                       City = _customer.City,
                       Contact = _customer.Contact
                   };
                   customers.Add(customer);
               }*/
           }
       }
       
    }
  
   
}
